/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.dao.impl;

import edu.fjs.liyq.dao.OrderDao;
import edu.fjs.liyq.entity.Order;
import edu.fjs.liyq.entity.OrderDetail;
import edu.fjs.liyq.util.DBUtil;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author danny
 */
public class OrderDaoImpl implements OrderDao {

    @Override
    public int insertOrder(Order order) {
        int result = 0;
        Connection con = DBUtil.getConnection();
        try {
            PreparedStatement pst = con.prepareStatement("insert into EASYBUY_ORDER(EO_ID,EO_USER_ID,EO_USER_NAME,EO_USER_ADDRESS,EO_CREATE_TIME,EO_COST,EO_STATUS,EO_TYPE)"
                    + " values(easybuy_all.nextval,?,?,?,?,?,?,?)");
//            pst.setLong(1, order.getEoId());
            pst.setString(1, order.getEoUserId());
            pst.setString(2, order.getEoUserName());
            pst.setString(3, order.getEoUserAddress());
            pst.setDate(4, new Date(order.getEoCreatTime().getTime()));
            pst.setDouble(5, order.getEoCost());
            pst.setInt(6, order.getEoStatus());
            pst.setInt(7, order.getEoType());
            result = pst.executeUpdate();
            
            // 将order在数据库中的ID值回写
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select easybuy_all.currval from dual");
            rs.next();
            order.setEoId(rs.getLong(1));
            System.out.println("======== "+ order.getEoId());
        } catch (SQLException ex) {
            Logger.getLogger(OrderDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int insertOrderDetail(OrderDetail orderDetail) {
        int result = 0;
        Connection con = DBUtil.getConnection();
        try {
            PreparedStatement pst = con.prepareStatement("insert into EASYBUY_ORDER_DETAIL(EOD_ID,EO_ID,EP_ID,EOD_QUANTITY,EOD_COST)"
                    + " values(easybuy_all.nextval,?,?,?,?)");
//            pst.setLong(1, order.getEoId());
            pst.setLong(1, orderDetail.getEoId());
            pst.setLong(2, orderDetail.getEpId());
            pst.setInt(3, orderDetail.getEodQuantity());
            pst.setDouble(4, orderDetail.getEodCost());
            result = pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(OrderDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public static void main(String[] args) {
        OrderDao dao = new OrderDaoImpl();
        Order order = new Order(Long.MIN_VALUE, "ss", new java.util.Date(), 99.0, 1, 1);
        dao.insertOrder(order);
    }
}
